import TermioPackage.*;

/**
 * This class is the console for the security control system. This process consists of two
 * threads. The SecurityMonitor object is a thread that is started that is responsible for the monitoring and control of
 * the security and fire systems. The main thread provides a text interface for the user to arm and disarm the
 * security system and to disable the sprinkler system when the fire alarm is triggered.
 * 
 * @author Kyle Benedict
 */
public class SecurityFireConsole {

    private static final boolean DEBUG_MODE = false;

    public static void main(String args[]) {
        Termio userInput = new Termio();	// Termio IO Object
        boolean done = false;   		// Main loop flag
        String option = null;           	// Menu choice from user
        SecurityMonitor sMonitor = null;        // The security monitor
        FireMonitor fMonitor = null;            // The fire monitor


        /////////////////////////////////////////////////////////////////////////////////
        // Get the IP address of the event manager
        /////////////////////////////////////////////////////////////////////////////////

        if (args.length != 0) {
            // event manager is not on the local system
            sMonitor = new SecurityMonitor(args[0]);
            fMonitor = new FireMonitor(args[0]);
        } else {
            sMonitor = new SecurityMonitor();
            fMonitor = new FireMonitor();
        } // if


        // Here we check to see if registration worked. If ef is null then the
        // event manager interface was not properly created.

        if (fMonitor.isRegistered() && sMonitor.isRegistered()) {
            new Thread(fMonitor).start();
            new Thread(sMonitor).start();

            if (args.length != 0) {
                System.out.println("Using event manager at: " + args[0]);
            } else {
                System.out.println("Using local event manager");
            }

            if (DEBUG_MODE) {
                System.out.println();
                System.out.println("WARNING!!  Compiled in debug mode.  Please " +
                        "recompile with DEBUG_MODE = false for the production " +
                        "version to disable test features");
            }

            while (!done) {
                System.out.println();
                System.out.println("-----------------------------------------");
                System.out.println("Security and Fire System Command Console");
                System.out.println("-----------------------------------------");


                System.out.println("Select an Option: \n");
                if (DEBUG_MODE) {
                    System.out.println(" D1: Enable sprinkler system [DEBUG_MODE ONLY]");
                    System.out.println(" D2: Enable fire alarm [DEBUG_MODE ONLY]");
                    System.out.println(" D3: Disable fire alarm [DEBUG_MODE_ONLY]");
                }

                System.out.println(" 1: Arm security system");
                System.out.println(" 2: Disarm security system");
                System.out.println(" 3: Confirm & engage sprinkler system");
                System.out.println(" 4: Disable sprinkler system");
                System.out.println(" X: Shutdown console & system\n");
                System.out.print(">> ");
                option = userInput.KeyboardReadString();

                if (DEBUG_MODE) {
                    if (option.equalsIgnoreCase("D1")) {
                        fMonitor.enableSprinklers();
                    } else if (option.equalsIgnoreCase("D2")) {
                        fMonitor.enableFireAlarm();
                    } else if (option.equalsIgnoreCase("D3")) {
                        fMonitor.disableFireAlarm();
                    }
                }
                if (option.equalsIgnoreCase("1")) {
                    sMonitor.armSecuritySystem();
                } else if (option.equalsIgnoreCase("2")) {
                    sMonitor.disarmSecuritySystem();
                } else if (option.equalsIgnoreCase("3")) {
                    fMonitor.confirmAndEngageSprinklers();
                } else if (option.equalsIgnoreCase("4")) {
                    fMonitor.disableSprinklers();
                } else if (option.equalsIgnoreCase("X")) {
                    sMonitor.halt();
                    fMonitor.halt();
                    done = true;
                    System.out.println("Console and system are halted...");
                } 
            }

        } else {
            System.out.println("\n\nUnable start the monitor.\n\n");
        }
    }
}

